//
//  SettingVC.m
//  Mahjong
//
//  Created by 饶首建 on 17/6/23.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "SettingVC.h"
#import "MahjongTypeCollectionViewCell.h"

#import <GizWifiSDK/GizWifiSDK.h>

@interface SettingVC () <UICollectionViewDelegate,UICollectionViewDataSource,GizWifiSDKDelegate,GizWifiDeviceDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *controlArr;

@property (nonatomic,copy) NSString *uid;
@property (nonatomic,copy) NSString *token;
@property (nonatomic,strong) GizWifiDevice *device;



@end

static NSString *kMahjongTypeCollectionViewCell = @"kMahjongTypeCollectionViewCell";
static NSString *kAppId = @"64eb58cff6764372bfeeda85ed4202ba";
static NSString *kAppSecret = @"c01ef05d9af14e35a891e69b5d46f328";

@implementation SettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [GizWifiSDK sharedInstance].delegate = self;
    [GizWifiSDK setLogLevel:GizLogPrintI];
//    [GizWifiSDK disableLAN:NO];
    //@[@"f3045d48f1b04831a8f3ac7787a711e6"]
    [GizWifiSDK startWithAppID:kAppId appSecret:kAppSecret specialProductKeys:nil cloudServiceInfo:nil autoSetDeviceDomain:NO];
    //匿名登录
    [[GizWifiSDK sharedInstance] userLoginAnonymous];
    
    _controlArr = [NSMutableArray new];
    [_controlArr addObjectsFromArray:@[@"掷骰子",@"关",@"开"]];
    [_collectionView registerClass:[MahjongTypeCollectionViewCell class] forCellWithReuseIdentifier:kMahjongTypeCollectionViewCell];    
}

#pragma mark - GizWifiDeviceDelegate
- (void)device:(GizWifiDevice *)device didReceiveData:(NSError *)result data:(NSDictionary *)dataMap withSN:(NSNumber *)sn{
    NSDictionary *data = dataMap[@"data"];
    if (data != nil && data.count != 0){
        NSNumber *switchNum = data[@"switch"];
        Log(@"%@",switchNum);
    }
}


#pragma mark - GizWifiSDKDelegate

- (void)wifiSDK:(GizWifiSDK *)wifiSDK didNotifyEvent:(GizEventType)eventType eventSource:(id)eventSource eventID:(GizWifiErrorCode)eventID eventMessage:(NSString *)eventMessage{
    if(eventType == GizEventSDK) {
        // SDK发生异常的通知
        NSLog(@"SDK event happened: [%@] = %@", @(eventID), eventMessage);
        
        NSArray *devList = [GizWifiSDK sharedInstance].deviceList;
        Log(@"%@",devList);
        
    } else if(eventType == GizEventDevice) {
        // 设备连接断开时可能产生的通知
        GizWifiDevice* mDevice = (GizWifiDevice*)eventSource;
        NSLog(@"device mac %@ disconnect caused by %@", mDevice.macAddress, eventMessage);
    } else if(eventType == GizEventM2MService) {
        // M2M服务返回的异常通知
        NSLog(@"M2M domain %@ exception happened: [%@] = %@", (NSString*)eventSource, @(eventID), eventMessage);
    } else if(eventType == GizEventToken) {
        // token失效通知
        NSLog(@"token %@ expired: %@", (NSString*)eventSource, eventMessage);
    }
}
- (void)wifiSDK:(GizWifiSDK *)wifiSDK didDiscovered:(NSError *)result deviceList:(NSArray *)deviceList {
    // 提示错误原因
    if(result.code != GIZ_SDK_SUCCESS) {
        NSLog(@"result: %@", result.localizedDescription);
    }
    // 显示变化后的设备列表
    NSLog(@"discovered deviceList: %@", deviceList);
    _device = deviceList.firstObject;
    
    if (_device.netStatus == GizDeviceOnline || _device.netStatus == GizDeviceControlled) {
        _device.delegate = self;
        [_device setSubscribe:nil subscribed:YES];//订阅
    }
}

- (void)wifiSDK:(GizWifiSDK *)wifiSDK didUserLogin:(NSError *)result uid:(NSString *)uid token:(NSString *)token {
    Log(@"%@",result);
    //注册设备
//    [[GizWifiSDK sharedInstance] bindDeviceWithUid:uid token:token did:@"EgVz3ZVEtCLvAvzNAeKnBh" passCode:@"123456" remark:nil];
//    [[GizWifiSDK sharedInstance] bindRemoteDevice:uid token:token mac:@"" productKey:@"" productSecret:@""];
    //主动刷新绑定设备列表、指定筛选的设备productKey
    [[GizWifiSDK sharedInstance] getBoundDevices:uid token:token specialProductKeys:nil];
}

#pragma mark - UICollectionViewDelegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _controlArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MahjongTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kMahjongTypeCollectionViewCell forIndexPath:indexPath];
    cell.titleLabel.text = _controlArr[indexPath.row];
    if (indexPath.row % 2) {
        cell.backgroundColor = [UIColor lightGrayColor];
    }else{
        cell.backgroundColor = [UIColor grayColor];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 4) {//关
        [self.device write:@{@"switch":@NO} withSN:5];
    }else if (indexPath.row == 5){//开
        [self.device write:@{@"switch":@YES} withSN:5];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(200,200);
}
/**
 *  section的内边距
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
/**
 *  item之间的行距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
/*
 *  item之间的左右间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
